﻿using System;
using Wise.TimeWarrior.DTO;
using Wise.TimeWarrior.Forms;
using Wise.TimeWarrior.Interfaces;

namespace Wise.TimeWarrior.Business.Impl
{
    /// <summary>
    /// <see cref="IConfigurationBusinessLogic"/>
    /// </summary>
    public class ConfigurationBusinessLogic : IConfigurationBusinessLogic
    {
        private ConfigurationForm configurationForm = null;

        /// <summary>
        /// <see cref="IConfigurationBusinessLogic.Configuration"/>
        /// </summary>
        public IConfiguration Configuration { set; private get; }

        /// <summary>
        /// <see cref="IConfigurationBusinessLogic.ShowConfigurationWindow"/>
        /// </summary>
        public BusinessItemDto ShowConfigurationWindow()
        {
            BusinessItemDto itemDto = new BusinessItemDto()
                {
                    Action = () =>
                                 {
                                     configurationForm.ConfigurationDto = Configuration.ReadConfiguration();
                                     configurationForm.Show();
                                 },
                    ActionOption = BusinessItemOption.Visible,
                    OriginalMethodName = "ShowConfigurationWindow"
                };
            return itemDto;
        }
        /// <summary>
        /// <see cref="IDisposable.Dispose"/>
        /// </summary>
        public void Dispose()
        {
            configurationForm.Dispose();
        }
        /// <summary>
        /// <see cref="IBLSetup.SetupLogic"/>
        /// </summary>
        public void SetupLogic()
        {
            configurationForm = new ConfigurationForm();
            configurationForm.SaveSettingsEvent += configurationForm_SaveSettingsEvent;
        }

        void configurationForm_SaveSettingsEvent(object sender, EventArgs e)
        {
            Configuration.SaveConfiguration(configurationForm.ConfigurationDto);
        }
    }
}
